package com.mxpio.erp.workshop.policy.impl;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.BomInfoService;
import com.mxpio.erp.common.technology.service.ItemPlanPropService;
import com.mxpio.erp.common.technology.service.ItemService;
import com.mxpio.erp.common.technology.vo.VersionVO;
import com.mxpio.erp.common.workshop.entity.OutsourceOrder;
import com.mxpio.erp.common.workshop.entity.OutsourceOrderItem;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class OutsourceOrderSavePolicy extends SmartCrudPolicyAdapter {
	
	@Autowired
    private BomInfoService bomInfoService;
	@Autowired
	private ItemPlanPropService itemPlanPropSerivce;
	@Autowired
	private ItemService itemSerivce;
	@Override
	public boolean beforeInsert(CrudContext context) {
		Object o = context.getEntity();
		if(o instanceof OutsourceOrder){
			return saveBomLine((OutsourceOrder) o);
		}
		return true;
	}
	
	@Override
	public boolean beforeUpdate(CrudContext context) {
		Object o = context.getEntity();
		if(o instanceof OutsourceOrder){
			OutsourceOrder order = JpaUtil.linq(OutsourceOrder.class).idEqual(((OutsourceOrder) o).getBizNo()).findOne();
			if(!order.getReferenceDate().equals(((OutsourceOrder) o).getReferenceDate())
					&&OrderStatusEnums.OrderStatus.PLAN.getStatusCode().equals(((OutsourceOrder) o).getOrderStatus())){
				return saveBomLine((OutsourceOrder) o);
			}
		}
		if(o instanceof OutsourceOrderItem){
			return false;
		}
		return true;
	}
	
	private boolean saveBomLine(OutsourceOrder o){
		String bizNo = ((OutsourceOrder) o).getBizNo();
		BigDecimal quantity = ((OutsourceOrder) o).getOrderQuantity();
		VersionVO vo = new VersionVO();
		vo.setDate(((OutsourceOrder) o).getReferenceDate());
		BomInfo bomInfo = bomInfoService.listBom(((OutsourceOrder) o).getProductItemCode(), vo,true);
		/*if(CollectionUtils.isEmpty(bomInfo.getBomInfoLineList())){//20221109 wupp，去掉保存时bominfo的bomInfoLineList不能为空的限制，允许在编辑bom中来修改对应的bom信息
			return false;
		}*/
		JpaUtil.lind(OutsourceOrderItem.class).equal("bizNo", bizNo).delete();
		int i = 0;
		if (bomInfo != null && bomInfo.getBomInfoLineList()!=null && bomInfo.getBomInfoLineList().size()>0){

			Set<String> itemCodeSet = bomInfo.getBomInfoLineList().stream().map(BomInfoLine::getItemCode).collect(Collectors.toSet());//bominfo中的物料

			List<Item> itemList = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
			Map<String, Item> itemMap = JpaUtil.index(itemList,"itemCode");//查询物料

			List<ItemMaterialProp> materialProps =  JpaUtil.linq(ItemMaterialProp.class).equal("propType", "item").in("code", itemCodeSet).list();//材料属性
			Map<String, ItemMaterialProp> materialPropMap = JpaUtil.index(materialProps,"code");

			List<ItemPlanProp> planProps =  JpaUtil.linq(ItemPlanProp.class).equal("propType", "item").in("code", itemCodeSet).list();//计划
			Map<String, ItemPlanProp> planPropMap = JpaUtil.index(planProps,"code");

			for(BomInfoLine line : bomInfo.getBomInfoLineList()){
				i++;
				OutsourceOrderItem item = new OutsourceOrderItem();
				item.setBizNo(bizNo);
				item.setQuantity(quantity.multiply(line.getItemConsumption()).setScale(2, RoundingMode.CEILING));
				item.setItemCode(line.getItemCode());
				item.setItemName(line.getItemName());
				item.setItemSpec(line.getItemSpec());
				item.setLineNo(i+"");
				//关联字段
				/*ItemPropKey key = new ItemPropKey();
				key.setPropType("item");
				key.setCode(line.getItemCode());
				ItemPlanProp itemPlanProp = itemPlanPropSerivce.getById(ItemPlanProp.class, key);*/
				ItemPlanProp itemPlanProp = planPropMap.get(line.getItemCode());
				if (itemPlanProp!=null){
					item.setFeedingMode(itemPlanProp.getFeedingMode());
				}
				ItemMaterialProp itemMaterialProp = materialPropMap.get(line.getItemCode());
				if (itemMaterialProp!=null){
					item.setMaterialType(itemMaterialProp.getMaterialType());//材料类型
				}
				//Item baseItem = itemSerivce.getById(Item.class, line.getItemCode());
				Item baseItem = itemMap.get(line.getItemCode());
				if (baseItem!=null){
					item.setUnitCode(baseItem.getUnitCode());
					item.setDrawingNo(baseItem.getDrawingNo());
					item.setWhCode(baseItem.getDefaultWhCode());//默认仓库
					item.setMaterialBrand(baseItem.getMaterialBrand());//材料牌号
				}
				//关联bominfoline的字段
				item.setMakeLength(line.getMakeLength());
				item.setMakeArea(line.getMakeArea());
				item.setMakeNum(line.getMakeNum());

				JpaUtil.save(item);
			}
		}
		return true;
	}
}
